package ibm.websphere.monitor.page;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * @copyright www.timotai.net
 * @author 这家伙真懒
 * @date 2016-1-12
 * <pre></pre>
 **/
public class WasPageImpl<T> extends WasPage<T>
{

	/**http请求对象*/
	private HttpServletRequest request;

	/**传递页码的参数名称*/
	private String pageNoKey;

	/**每页显示的结果集*/
	private int pageSize;

	/**总页数*/
	private int pageCount;

	/**总记录数*/
	private int recordCount;

	/**当前页码*/
	private int pageNo;

	/**记录读取的开始位置**/
	private int offset;

	/**
	 * 分页的URL模板：如http://sample.com/list.do?pageNo=23&name=sample&kw=test
	 *
	 * pageNoKey 为 pageNo
	 *
	 * 分页的URL模板为： http://sample.com/list.do?pageNo=#{pageNo}&name=sample&kw=test
	 *
	 */
	private String pageURL;

	private List<T> dataList;


	public WasPageImpl(
			HttpServletRequest request, String pageNoKey, int maxResult)
	{
		this.request = request;

		if (pageNoKey == null) {
			this.pageNoKey = WasPage.DEFAULT_PAGENO_KEY;
		} else {
			this.pageNoKey = pageNoKey;
		}

		if (maxResult == 0) {
			this.pageSize = WasPage.DEFAULT_PAGESIZE;
		} else {
			this.pageSize = maxResult;
		}

		try {
			this.pageNo = Integer.parseInt(request.getParameter(pageNoKey));
		} catch (NumberFormatException ex) { //非法的页码
			this.pageNo = 1;
		}

		this.offset = _calculateOffset(this.pageNo, this.pageSize);
	}

	@Override
	public void execute(PageResult<T> pageResult) {
		// TODO Auto-generated method stub

		//获取当前的结果的总记录数
		this.recordCount = pageResult.getNumberObjects();

		//分页结果集
		this.dataList    = pageResult.getData();

		//计算总页数
		int temp = this.recordCount % this.pageSize;
		if (temp == 0) {
			this.pageCount = this.recordCount / this.pageSize;
		} else {
			this.pageCount = this.recordCount / this.pageSize + 1;
		}

		if (this.pageNo > this.pageCount) {
			this.pageNo = this.pageCount;
		}

		this.pageURL = _toURLTemplate(request.getRequestURI(),
				request.getQueryString());

		//保存分页信息到request中
		this.request.setAttribute("WasPage", this);
	}

	public void execute(List<T> dataList, int numberObjects ) {
		this.execute(new PageResult<T>(dataList, numberObjects));
	}

	private int _calculateOffset(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}

	private String _toURLTemplate(
			String requestURI, String queryString)
	{
		String pageNoPair = pageNoKey + "=" + pageNo;

		String pattern = pageNoPair + "(?:&)?";
		if (queryString == null) {
			queryString = pageNoPair;
		} else if( queryString.indexOf(pageNoPair) == -1) {
			queryString += "&"+pageNoPair;
		}

		queryString = queryString.replaceAll(pattern,
				pageNoKey + "="+ PAGENO_REPLACEMENT +"&");
//		LogUtils.info("IDEA业务平台", "requestURI:" +  requestURI);
		return requestURI +
				("".equals(queryString.trim()) ? "" :
						("?" + queryString));
	}

	/***getters*/

	public int getOffset() {
		return this.offset;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public int getPageCount() {
		return this.pageCount;
	}

	public String getPageURL() {
		return this.pageURL;
	}

	public int getRecordCount() {
		return this.recordCount;
	}

	public int getPageNo() {
		return this.pageNo;
	}

	public String getPageNoKey() {
		return this.pageNoKey;
	}

	public List<T> getDataList() {
		return this.dataList;
	}

	public static void main(String[] args) {

		String regex = "pageNo=23(?:&)?";
		String input = "pageNo=23&name=32323&sample=323";
		System.out.println(input.replaceAll("pageNo=23(?:&)?", "pageNo=#{pageNo}&"));
		System.out.println(Pattern.matches(regex, input));
	}

}
